<template>
  <el-col>
    <el-dialog
      title="增加"
      :visible.sync="addDialogVisible"
      :before-close="handleCloseAddDialog"
    >
      <el-form
        ref="patientInfo"
        :model="patientInfo"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="患者名字" prop="name">
          <el-input v-model="patientInfo.name" />
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="patientInfo.username" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="patientInfo.password" type="password" />
        </el-form-item>
        <el-form-item label="身份证" prop="id">
          <el-input v-model="patientInfo.id" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleCloseAddDialog">取 消</el-button>
        <el-button type="primary" @click="handleConfirmAddDialog"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <el-dialog
      title="更新"
      :visible.sync="updateDialogVisible"
      :before-close="handleCloseUpdateDialog"
    >
      <el-form
        ref="patientInfo"
        :model="patientInfo"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="患者名字" prop="name">
          <el-input v-model="patientInfo.name" />
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="patientInfo.username" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="patientInfo.password" type="password" />
        </el-form-item>
        <el-form-item label="身份证" prop="id">
          <el-input v-model="patientInfo.id" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleCloseUpdateDialog">取 消</el-button>
        <el-button type="primary" @click="handleConfirmUpdateDialog"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <el-row>
      <el-col :span="2">
        <el-button type="primary" @click="addDialogVisible = true"
          >增加</el-button
        >
      </el-col>

      <el-col :span="2">
        <el-button
          :disabled="multipleSelection.length > 1"
          type="warning"
          @click="showUpdateDialogVisible"
          >更新</el-button
        >
      </el-col>

      <el-col :span="2">
        <el-button type="danger" @click="handleDeleteSelectIds">删除</el-button>
      </el-col>

      <el-col :span="5">
        <el-input v-model="query.name" type="text" placeholder="患者名" />
      </el-col>
      <el-col :span="5">
        <el-input v-model="query.id" type="text" placeholder="身份证" />
      </el-col>
      <el-col :span="5">
        <el-input v-model="query.username" type="text" placeholder="用户名" />
      </el-col>
      <el-col :span="2">
        <el-button @click="queryClear"> 清除 </el-button>
      </el-col>
      <el-col :span="2">
        <el-button @click="doSelectAll"> 搜索 </el-button>
      </el-col>
    </el-row>

    <el-table
      ref="multipleTable"
      :data="patientList"
      style="width: 100%"
      row-key="patientId"
      lazy
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column prop="patientId" label="患者 ID" />
      <el-table-column prop="name" label="患者名" />
      <el-table-column prop="username" label="用户名" />
      <el-table-column prop="id" label="身份证" />
    </el-table>

    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      @prev-click="currentChange"
      @next-click="currentChange"
      @current-change="currentChange"
    />
  </el-col>
</template>
  
  <script>
export default {
  name: "",
  data() {
    return {
      addDialogVisible: false,
      updateDialogVisible: false,

      patientList: [],
      patientTypeList: [],

      page: 0,
      pageSize: 10,
      total: 0,

      multipleSelection: [],

      patientInfo: {
        name: '',
        username: '',
        password: '',
        id: '',
      },

      rules: {
        name: [{ required: true, trigger: "blur" }],
        username: [{ required: true, trigger: "blur" }],
        password: [{ required: true, trigger: "blur" }],
        id: [{ required: true, trigger: "blur" }],
      },

      query: {
        name: "",
        username: "",
        id: "",
      },
    };
  },
  methods: {
    currentChange(index) {
      this.page = index - 1;
    },

    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    queryClear() {
      this.query = {
        name: "",
        username: "",
        id: "",
      };
      this.doSelectAll();
    },

    doSelectAll() {
      this.$store
        .dispatch("patient/selectAll", {
          obj: {
            name: this.query.name,
            username: this.query.username,
            id: this.query.id,
          },
          page: this.page,
          pageSize: this.pageSize,
        })
        .then((res) => {
          this.patientList = res.list;
          this.total = res.total;
        });
    },

    handleConfirmAddDialog() {
      this.$store.dispatch("patient/add", this.patientInfo);
      this.addDialogVisible = false;
      this.refresh();
    },
    handleCloseAddDialog() {
      this.addDialogVisible = false;
      this.refresh();
    },

    showUpdateDialogVisible() {
      if (this.multipleSelection.length === 0) {
        this.$message.error("请选择");
        return;
      }
      this.patientInfo = this.multipleSelection[0];
      this.updateDialogVisible = true;
    },
    handleConfirmUpdateDialog() {
      this.$store.dispatch("patient/update", this.patientInfo);
      this.updateDialogVisible = false;
      this.refresh();
    },
    handleCloseUpdateDialog() {
      this.updateDialogVisible = false;
      this.refresh();
    },

    handleDeleteSelectIds() {
      const ids = [];
      for (const obj of this.multipleSelection) {
        ids.push(obj.patientId);
      }
      this.$store.dispatch("patient/delete", ids).then(() => {});
      this.doSelectAll();
    },

    refresh() {
      this.patientInfo = {
        name: '',
        username: '',
        password: '',
        id: '',
      };
      this.doSelectAll()
      this.$refs.multipleTable.clearSelection();
    },
  },
  created() {
    this.doSelectAll();
  },
};
</script>
  
  <style scoped>
</style>